// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
//
function onDeviceReady() {
	$("#txtUUID").val(device.uuid);
}

// function Reset ( username, password )
function resetUI() {
	$("#txtUsername").val('');
	$("#txtPassword").val('')
	$("#txtUsername").focus();
}

// function login 
function login() {
	window.location="main.html"; return;
	var username = $("#txtUsername").val();
	var password = $("#txtPassword").val();
	var uuid	 = $("#txtUUID").val();
	if (username=='undefined' || username=='') {
		alert('Please, input username !');
		return;
	}
	if (password=='undefined' || password=='') {
		alert('Please, input password !');
		return;
	}
	var result = callLoginService(username,password,uuid);
	if (result == "existdata") {
		$("#strMessage").html("Login sucessfull");
		window.location="main.html";
	} else if (result == "passnotvalid") {
//		txtMessage.val("正しいパスワードを入力して下さい。");
		$("#strMessage").html("passnotvalid");
	} else if (result == "pcnotvalid") {
//		alert("PCが有効ではありません。管理者に要求を送り出してください。");
		alert("pcnotvalid");
		$("#strMessage").html("pcnotvalid");
		$("#btnSendReq").attr("disabled", "disabled");
	} else if (result == "nodata") {
		$("#strMessage").html("Login fail");
//		alert("正しい教職員コードを入力して下さい。");
		alert("Login fail");
		$("#btnSendReq").removeAttr("disabled");
	}
}

// call service Login
function callLoginService(username,password,uuid) {
	var curURL = "http://192.168.1.38:8989/mfscWS/services/UserAuthen/checkUserValid?strUsername=";
	curURL = curURL + username + "&strPassword=" + password + "&strUUID=" + uuid;
	var result = "";
	$.ajax({
        type: "POST",
        async: false,	
        url: curURL,
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        dataType: "xml",
        timeout: 30000,		
        success: function (data, textStatus, jqXHR){  
        	result = $(data).text();
        } ,
        error: function (xhr, ajaxOptions, thrownError){  
            alert(xhr.status);          
            alert(thrownError);
        } 
    });
	return result;
}

// function Send Request
function sendRequest() {
	var username = $("#txtUsername").val();
	var uuid	 = $("#txtUUID").val();
	if (username=='undefined' || username=='') {
		alert('Please, input username !');
		return;
	}
	var result = callRequestService(username,uuid);
	if ('ok'==result) {
		$("#strMessage").html("Success!");
		alert("Success");
//		alert("要求が 送り出しました。");
		$("#btnSendReq").attr("disabled", "disabled");
	} else {
		$("#strMessage").html("Fail!");
		alert("Fail");
//		alert("もう一度要求を送り出してみてください。");
		$("#btnSendReq").removeAttr("disabled");
	}
}

function callRequestService(username,uuid) {
	var curURL = "http://192.168.1.38:8989/mfscWS/services/UserAuthen/sendRequest?strUsername=";
	curURL = curURL + username + "&strUUID=" + uuid;
	var result = "";
	$.ajax({
        type: "GET",
        async: false,	
        url: curURL,
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        dataType: "xml",
        timeout: 30000,		
        success: function (data, textStatus, jqXHR){  
        	result = $(data).text();
        } ,
        error: function (xhr, ajaxOptions, thrownError){  
            alert(xhr.status);          
            alert(thrownError);
        } 
    });
	return result;
}